/**
 * author: zhou
 * date: 2017.10.23
 * description: 角色管理中心
 * functions: {
 *      save: 保存角色信息
 *      findAll: 获取全部角色
 *      findOne: 获取角色信息
 *      update: 更新角色
 * }
 * 
 */

const   mongoPromise = require('../../../core/mongo/mongoPromise'),
        Role         = require('../../../core/mongo/models/school/roleModel'),
        Menu         = require('../../../core/mongo/models/system/menuModel');

const roleController = {
    /**
     * 保存角色信息
     * @param {*} ctx 
     */
    async save(ctx) {
        const { name, schoolId, menuIds } = ctx.request.body.data;
        if (!name) {
            ctx.body = { success: false, message: '角色名称不能为空', state: 10004 };
            return;
        }
        if (!schoolId) {
            ctx.body = { success: false, message: '学校id不能为空', state: 10004 };
            return;
        }
        if (!menuIds || !Array.isArray(menuIds) || menuIds.length === 0) {
            ctx.body = { success: false, message: '角色关联菜单不能为空', state: 10004 };
            return;
        }
        const role = new Role({
            name,
            schoolId,
            menuIds
        });
        await Role.create(role).then((doc) => {
            ctx.body = { success: true, data: [], state: 200 };
        },
        (error) => {
            ctx.body = { success: false, message: '抱歉,保存角色失败', state: 10003 };
        });
    },
    /**
     * 获取角色列表
     * @param {*} ctx 
     */
    async findAll(ctx) {
        const { schoolId, date, limit } = ctx.request.body.data;
        if (!schoolId) {
            ctx.body = { success: false, message: '学校id不能为空', state: 10004 };
            return;
        }
        const queryParam = {
            schoolId
        };
        if (date) {
            queryParam.createDate = { $gt: date };
        }
        let query = Role.find(queryParam);
        if (limit) { query = query.limit(limit); }
        query = query.populate('menuIds', { _id: 0, name: 1 });
        const result = await mongoPromise(ctx, query);
        if (result) {
            const countQuery = Role.count({ schoolId });
            const countResult = await mongoPromise(ctx, countQuery);
            if (countResult !== undefined) {
                ctx.body = { success: true, data: { data: result, count: countResult }, state: 200 };
            }
        }
    },
    /**
    * 查询角色信息
    * @param {*} ctx 
    */
    async findOne(ctx) {
        const { id } = ctx.request.body.data;
        if (!id) {
            ctx.body = { success: false, message: '角色id不能为空', state: 10004 };
            return;
        }
        const query = Role.findById(id);
        const result = await mongoPromise(ctx, query);
        if (result !== undefined) {
            ctx.body = { success: true, data: result, state: 200 };
        } else {
            ctx.body = { success: false, message: '抱歉,未查找到该角色', state: 200 };
        }
    },
    /**
     * 更新角色信息
     * @param {*} ctx 
     */
    async update(ctx) {
        const { _id, name, schoolId, menuIds, } = ctx.request.body.data;
        if (!_id) {
            ctx.body = { success: false, message: '角色id不能为空', state: 10004 };
            return;
        }
        if (!name) {
            ctx.body = { success: false, message: '角色名称不能为空', state: 10004 };
            return;
        }
        if (!schoolId) {
            ctx.body = { success: false, message: '学校id不能为空', state: 10004 };
            return;
        }
        if (!menuIds || !Array.isArray(menuIds) || menuIds.length === 0) {
            ctx.body = { success: false, message: '角色关联菜单不能为空', state: 10004 };
            return;
        }
        const role = {
            name,
            schoolId,
            menuIds
        };
        const query = Role.update({ _id }, { $set: role });
        const result = await mongoPromise(ctx, query);
        if (result) {
            ctx.body = { success: true, data: [], state: 200 };
        }
    },
    /**
     * 获取菜单列表
     * @param {*} ctx 
     */
    async menus(ctx) {
        const query = Menu.find();
        const result = await mongoPromise(ctx, query);
        if (result) {
            ctx.body = { success: true, data: result, state: 200 };
        }
    }
};

module.exports = roleController;